package se.grp03.fmtuner.scheduler.job;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import se.grp03.fmtuner.scheduler.io.IOInterface;
import se.grp03.fmtuner.scheduler.io.UdpManager;

public class TuneJob implements Job {

	String message = null;
	//IOInterface io;
	
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		// Retrieve tune message passed to job.
		JobDataMap dataMap = arg0.getMergedJobDataMap();
		//io = (IOInterface) dataMap.get("IO_INTERFACE");
		//System.out.println("IO_INTERFACE 2: " + io.toString()); // <-- DEBUG --------------
		message = (String) dataMap.get("TUNE_MESSAGE");
		//System.out.println("MESSAGE: " + message); // <-- DEBUG -------------------------
		
		// Step 1: Open connection with FM Tuner Agent.
		//setIOInterface();
		
		// Step 2: Send tune message to Agent.
		//io.doWrite(message);
		UdpManager udp = UdpManager.getInstance();
		udp.tune(message);
		
		// Step 3: Get response from Agent.
		//String response = io.doRead();
		
		// Step 4: Close connection with Agent.
		//io.doClose();
	}
	
	/**
	 * Establishes the UDP port number, and sets the tune message to send.
	 */
	/*void setIOInterface() {
		if(io == null) {
			io = new UdpAdapter(UdpConstants.AGENT_PORT);
			io.doOpen();
		}
	}*/
}